package com.example.springbootthymeleafintegrat.web;


import com.example.springbootthymeleafintegrat.character.CharacterClass;
import com.example.springbootthymeleafintegrat.character.CharacterInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Controller
public class CreationController {

    private static final HashMap<String, CharacterClass> characterMap = new HashMap<>();

    static {
        characterMap.put(CharacterClass.ASSASSIN.getText(), CharacterClass.ASSASSIN);
        characterMap.put(CharacterClass.WARRIOR.getText(), CharacterClass.WARRIOR);
        characterMap.put(CharacterClass.WIZARD.getText(), CharacterClass.WIZARD);
    }

    @GetMapping("/")
    public String index() {
        return "start";
    }

    @GetMapping("/create/1")
    public String createNameForm() {
        return "name";
    }

    @PostMapping("/create/1")
    public String createStep1(HttpServletRequest request, String charactername) {
        if (charactername == null || charactername.isBlank()) return "name";
        request.getSession().setAttribute("name", charactername);
        return "redirect:/create/2";
    }

    @GetMapping("/create/2")
    public String createClassForm(@SessionAttribute("name") String name, Model m) {
        CharacterInfo character = new CharacterInfo(name);
        m.addAttribute("character", character);
        return "class";
    }

    @PostMapping("/create/2")
    public String createStep3(HttpServletRequest request, CharacterClass characterclass) {
        CharacterInfo character = new CharacterInfo("Laime Jannister");
        character.setCharacterClass(characterMap.get(characterclass.getText()));
        request.getSession().setAttribute("characterInfo", character);
        return
                "redirect:/create/3";
    }

    @GetMapping("/create/3")
    public String createAttributeForm(@SessionAttribute("characterInfo") CharacterInfo characterInfo, Model m) {
//        CharacterInfo character = new CharacterInfo("Laime Jannister");
//        character.setCharacterClass(CharacterClass.WARRIOR);
        m.addAttribute("character", characterInfo);
        return "attributes";
    }

    @PostMapping("/create/3")
    public String createStep3(Model m,
                              @RequestParam("Strength") int str,
                              @RequestParam("Dexterity") int dex,
                              @RequestParam("Constitution") int con,
                              @RequestParam("Intelligence") int intl,
                              @RequestParam("Wisdom") int wis,
                              @RequestParam("Charisma") int cha) {

        // Character fertigstellen und validieren

        // Fehler: Formular wieder anzeigen, Werte müssen erhalten bleiben

        // Erfolg: Ausgabe der Spielerinformationen auf der Konsole und zurück zur Startseite

        return "redirect:/";
    }

}
